package com.adgox.tiantianbiting.redpacket.rank_income;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.adapter.RankingIncomeAdapter;
import com.adgox.tiantianbiting.bean.BaseResponseBean2;
import com.adgox.tiantianbiting.bean.RankIncomeBean;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RankingIncomeActivity extends AppCompatActivity {

    @BindView(R.id.tv_day)
    TextView tvDay;
    @BindView(R.id.tv_week)
    TextView tvWeek;
    @BindView(R.id.tv_month)
    TextView tvMonth;
    @BindView(R.id.rv)
    RecyclerView rv;
    private RankingIncomeAdapter adapter;
    private int currentChoosen = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking_income);
        ButterKnife.bind(this);
        getRecyclerviewData("day");
    }


    @OnClick(R.id.tv_day)
    public void onDayClicked() {
        if (currentChoosen == 1) return;
        currentChoosen = 1;
        setAllUnselected();
        tvDay.setBackgroundResource(R.drawable.bg_btn_ranking_income_selected);
        getRecyclerviewData("day");
    }

    @OnClick(R.id.tv_week)
    public void onWeekClicked() {
        if (currentChoosen == 2) return;
        currentChoosen = 2;
        setAllUnselected();
        tvWeek.setBackgroundResource(R.drawable.bg_btn_ranking_income_selected);
        getRecyclerviewData("week");
    }

    @OnClick(R.id.tv_month)
    public void onMonthClicked() {
        if (currentChoosen == 3) return;
        currentChoosen = 3;
        setAllUnselected();
        tvMonth.setBackgroundResource(R.drawable.bg_btn_ranking_income_selected);
        getRecyclerviewData("month");
    }

    private void setAllUnselected() {
        tvDay.setBackgroundColor(Color.TRANSPARENT);
        tvWeek.setBackgroundColor(Color.TRANSPARENT);
        tvMonth.setBackgroundColor(Color.TRANSPARENT);

    }

    private void getRecyclerviewData(String type) {
        DialogUtils.showLoadingDialog(this);
        OkGo.<ArrayList<BaseResponseBean2<RankIncomeBean>>>get(ApiConstant.BASE_URL + ApiConstant.RANKING_INCOME)
                .params("type", type)
                .execute(new JsonCallback<ArrayList<BaseResponseBean2<RankIncomeBean>>>() {
                    @Override
                    public void onSuccess(Response<ArrayList<BaseResponseBean2<RankIncomeBean>>> response) {
                        DialogUtils.dismiss();
                        if (response.body() == null) {
                            Toast.makeText(RankingIncomeActivity.this, "获取排行榜数据出错", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        BaseResponseBean2<RankIncomeBean> bean = response.body().get(0);
                        if (bean == null || bean.getCode() != 0) {
                            Toast.makeText(RankingIncomeActivity.this, "获取排行榜数据出错", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        ArrayList<RankIncomeBean> list = bean.getList();
                        initRecyclerview(list);
                    }
                });
    }

    private void initRecyclerview(ArrayList<RankIncomeBean> list) {
        if (adapter != null) adapter.setDatas(null);
        LinearLayoutManager lm = new LinearLayoutManager(this);
        rv.setLayoutManager(lm);
        adapter = new RankingIncomeAdapter(list);
        rv.setAdapter(adapter);
    }

    @OnClick(R.id.img_finish)
    public void onFinishClicked() {
        finish();
    }
}
